package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdut.material.pojo.Material;
import com.sdut.material.pojo.Repository;
import com.sdut.material.mapper.RepositoryMapper;
import com.sdut.material.pojo.Supplier;
import com.sdut.material.pojo.SupplierMaterial;
import com.sdut.material.pojo.vo.RepositoryVO;
import com.sdut.material.service.IMaterialService;
import com.sdut.material.service.IRepositoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.service.ISupplierMaterialService;
import com.sdut.material.service.ISupplierService;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class RepositoryServiceImpl extends ServiceImpl<RepositoryMapper, Repository> implements IRepositoryService {
    @Autowired
    private RepositoryMapper repositoryMapper;
    @Autowired
    private ISupplierMaterialService supplierMaterialService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IMaterialService materialService;
    @Override
    public int selectSumByMaterialId(int materialId) {
        return repositoryMapper.selectSumByMaterialId(materialId);
    }

    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        long offset = (page - 1) * limit;
        List<Repository> repositoryList = repositoryMapper.selectByPage(offset,limit);
        int totalCount = repositoryMapper.selectTotalCount();
        List<RepositoryVO> repositoryVOList = new ArrayList<>();
        for (Repository repository : repositoryList) {
            RepositoryVO repositoryVO = new RepositoryVO();
            repositoryVO.setId(repository.getId());
            SupplierMaterial supplierMaterial = supplierMaterialService.selectById(repository.getSumaId());
            Supplier supplier = supplierService.selectById(supplierMaterial.getSupplierId());
            Material material = materialService.selectById(supplierMaterial.getMaterialId());
            repositoryVO.setSupplierName(supplier.getName());
            repositoryVO.setMaterialName(material.getName());
            repositoryVO.setCount(repository.getCount());
            repositoryVO.setShelflife(repository.getShelflife());
            repositoryVOList.add(repositoryVO);
        }
        return LayUITableResult.ok((long) totalCount,repositoryVOList);
    }

    @Override
    public void deleteById(Integer repositoryId) {
        repositoryMapper.deleteById(repositoryId);
    }

    @Override
    public Repository selectBySupplierMaterialId(int supplierMaterialId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("suma_id",supplierMaterialId);
        Repository repository = repositoryMapper.selectOne(queryWrapper);
        return repository;
    }

    @Override
    public void insert(Repository newRepository) {
        repositoryMapper.insert(newRepository);
        return;
    }

    @Override
    public void updateRepository(Repository repository) {
        repositoryMapper.updateById(repository);
        return;
    }
}
